package com.suse.controller;


import com.suse.model.param.LoginParam;
import com.suse.model.vo.UserVo;
import com.suse.entity.UserInfo;
import com.suse.result.Result;
import com.suse.service.UserInfoService;
import com.suse.service.UserService;
import com.suse.util.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.suse.result.ResultEnum.RESULT_FUNCTION_NOT_FINISH;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private UserInfoService userInfoService;

    /**
     * 发送验证码
     * @param phone 手机号
     * @return 发送验证码是否成功
     */
    @PostMapping("/code")
    public Result sendCode(@RequestParam("phone") String phone) {
        return userService.sendCode(phone);
    }

    /**
     * 登录功能
     * @param loginParam 登录表单数据，包含手机号、验证码；或者手机号、密码
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginParam loginParam){
        return userService.login(loginParam);
    }

    /**
     * 登出功能
     * @return 无
     */
    @PostMapping("/logout")
    public Result logout(){
        return Result.error(RESULT_FUNCTION_NOT_FINISH);
    }

    @GetMapping("/me")
    public Result me(){
        UserVo user = UserHolder.getUser();
        return Result.success(user);
    }

    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long userId){
        // 查询详情
        UserInfo info = userInfoService.getById(userId);
        if (info == null) {
            // 没有详情，应该是第一次查看详情
            return Result.success();
        }
        info.setCreateTime(null);
        info.setUpdateTime(null);
        // 返回
        return Result.success(info);
    }

}
